package com.keyue.service.admin.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.keyue.common.constant.Constant;
import com.keyue.common.constant.ServerResult;
import com.keyue.domain.dto.BaseDO;
import com.keyue.domain.dto.admin.AdminDeptDO;
import com.keyue.domain.vo.admin.AdminDeptQueryVO;
import com.keyue.domain.vo.admin.AdminDeptResultVO;
import com.keyue.domain.vo.admin.AdminDeptVO;
import com.keyue.mapper.admin.AdminDeptMapper;
import com.keyue.service.admin.AdminDeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 部门表(AdminDept)表服务实现类
 *
 * @author makejava
 * @since 2023-08-30 17:51:48
 */
@Slf4j
@Service
public class AdminDeptServiceImpl extends ServiceImpl<AdminDeptMapper, AdminDeptDO> implements AdminDeptService {


    /**
     * 通过ID查询单条数据
     * <p>
     * adminDeptQueryVO 主键
     *
     * @return 实例对象
     */
    @Override
    public ServerResult queryById(AdminDeptQueryVO adminDeptQueryVO) {
        if (Objects.isNull(adminDeptQueryVO.getId())) {
            return ServerResult.fail("id 不能为空");
        }
        AdminDeptDO AdminDeptDo = getById(adminDeptQueryVO.getId());
        if (Objects.isNull(AdminDeptDo)) {
            return ServerResult.success(null);
        }
        return ServerResult.success(BeanUtil.copyProperties(AdminDeptDo, AdminDeptVO.class));
    }

    /**
     * 分页查询
     *
     * @param queryVO 筛选条件
     * @return 查询结果
     */
    @Override
    public ServerResult list(AdminDeptQueryVO queryVO) {

        LambdaQueryWrapper<AdminDeptDO> queryWrapper = buildQueryByParam(queryVO);
        Page page = new Page<>(queryVO.getPage(), queryVO.getPageSize());
        IPage<AdminDeptDO> iPage = page(page, queryWrapper);

        Page<AdminDeptResultVO> webVOPage = new Page<>();
        List<AdminDeptDO> adminDeptDOS = iPage.getRecords();
        if (CollectionUtils.isNotEmpty(adminDeptDOS)) {
            List<AdminDeptResultVO> queryVos = adminDeptDOS.stream().map(x -> {
                AdminDeptResultVO resultVO = new AdminDeptResultVO();
                BeanUtils.copyProperties(x, resultVO);
                return resultVO;
            }).collect(Collectors.toList());
            webVOPage.setRecords(queryVos);
        }
        BeanUtils.copyProperties(page, webVOPage, "records");
        return ServerResult.success(webVOPage);
    }

    /**
     * @param queryVO
     * @return
     */
    private LambdaQueryWrapper<AdminDeptDO> buildQueryByParam(AdminDeptQueryVO queryVO) {
        return new LambdaQueryWrapper<AdminDeptDO>()
                .eq(BaseDO::getEnableFlag, Constant.ENABLE_TEN)
                .eq(StringUtils.isNotBlank(queryVO.getName()), AdminDeptDO::getName, queryVO.getName());
    }

    /**
     * 新增数据
     *
     * @param adminDeptVO 实例对象
     * @return 实例对象
     */
    @Override
    public ServerResult add(AdminDeptVO adminDeptVO) {
        //todo 校验
        if (Objects.nonNull(adminDeptVO.getId())) {
            adminDeptVO.setId(null);
        }
        AdminDeptDO adminDeptDo = BeanUtil.copyProperties(adminDeptVO, AdminDeptDO.class);

        save(adminDeptDo);

        return ServerResult.success();
    }

    /**
     * 修改数据
     *
     * @param adminDeptVO 实例对象
     * @return 实例对象
     */
    @Override
    public ServerResult edit(AdminDeptVO adminDeptVO) {
        //todo 校验
        if (Objects.isNull(adminDeptVO.getId())) {
            return ServerResult.fail("id 不能为空");
        }
        AdminDeptDO adminDeptDo = BeanUtil.copyProperties(adminDeptVO, AdminDeptDO.class);

        updateById(adminDeptDo);

        return ServerResult.success();
    }

    /**
     * 通过主键删除数据
     *
     * @param adminDeptQueryVO 筛选条件
     * @return 是否成功
     */
    @Override
    public ServerResult deleteById(AdminDeptQueryVO adminDeptQueryVO) {
        List<Long> ids = adminDeptQueryVO.getIds();
        if (CollUtil.isEmpty(ids)) {
            return ServerResult.fail("ids 参数不能为空");
        }
        List<AdminDeptDO> adminDeptList = listByIds(ids);
        if (CollUtil.isEmpty(adminDeptList)) {
            return ServerResult.success();
        }
        //todo 校验是否可以逻辑删除
        LambdaUpdateWrapper<AdminDeptDO> updateWrapper = new LambdaUpdateWrapper<AdminDeptDO>()
                .in(AdminDeptDO::getId, ids)
                .set(AdminDeptDO::getEnableFlag, Constant.UNABLE_TWENTY);
        update(updateWrapper);
        return ServerResult.success();
    }
}
